package pso.gui;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import org.jfree.chart.ChartPanel;

import pso.gui.icons.IconLoader;
import pso.gui.visualization.SwarmVisualizationPanel;

import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;

public class ProgressDialog extends JFrame {

	private static final String ROW_SPECS = "pref:grow, 3dlu, 350, 3dlu, 80dlu:grow, 3dlu, pref, 3dlu, pref";
	private static final String COL_SPECS = "350, 3dlu, pref:grow";

	private static final long serialVersionUID = 1329942569497777771L;

	private JProgressBar progressBar = new JProgressBar();
	private ChartPanel convergenceChartPanel = new ChartPanel(null);
	private SwarmVisualizationPanel swarmVisualizationPanel;
	private JTextArea summaryTextArea = new JTextArea();
	private JButton closeButton = new JButton();

	public ProgressDialog() {
		setTitle("MCPSO - Podsumowanie");
		setIconImage(IconLoader.getIcon("summary64.png"));
	}

	public void initGui() {
		FormLayout formLayout = new FormLayout(COL_SPECS, ROW_SPECS);
		PanelBuilder builder = new PanelBuilder(formLayout);

		builder.add(progressBar, CC.xy(1, 1, "fill, fill"));
		builder.add(convergenceChartPanel, CC.xywh(3, 1, 1, 3));
		builder.add(swarmVisualizationPanel, CC.xy(1, 3));

		convergenceChartPanel.getChart().setBackgroundPaint(
				UIManager.getDefaults().getColor("Panel.background"));

		summaryTextArea.setEditable(false);
		builder.add(new JScrollPane(summaryTextArea),
				CC.xyw(1, 5, 3, "fill, fill"));

		builder.addSeparator("", CC.xyw(1, 7, 3));
		JPanel buttonPanel = new ButtonBarBuilder().addButton(closeButton)
				.build();
		builder.add(buttonPanel, CC.xyw(1, 9, 3));
		builder.setDefaultDialogBorder();

		getContentPane().add(new JScrollPane(builder.getPanel()));
	}

	public ChartPanel getConvergenceChartPanel() {
		return convergenceChartPanel;
	}

	public SwarmVisualizationPanel getSwarmVisualizationPanel() {
		return swarmVisualizationPanel;
	}

	public void setSwarmVisualizationPanel(
			SwarmVisualizationPanel swarmVisualizationPanel) {
		this.swarmVisualizationPanel = swarmVisualizationPanel;
	}

	public JProgressBar getProgressBar() {
		return progressBar;
	}

	public JButton getCloseButton() {
		return closeButton;
	}

	public JTextArea getSummaryTextArea() {
		return summaryTextArea;
	}
}
